<?php
interface Handler
{
    /** @param Handler $next */
    public function setNextHandler($next);
    public function handlerRequest($type,$message);
}

class HandlerA implements Handler
{
    public function setNextHandler($next)
    {
        $this->next = $next;
        return $this;
    }
    public function handlerRequest($type, $message)
    {
        if($type === "a"){
            echo "HandlerA:$message\r\n";
        }
        elseif($this->next){
            return $this->next->handlerRequest($type, $message);
        }
    }
}

class HandlerB implements Handler
{
    public function setNextHandler($next)
    {
        $this->next = $next;
        return $this;
    }
    public function handlerRequest($type, $message)
    {
        if($type === "b"){
            echo "HandlerB:$message\r\n";
        }
        elseif($this->next){
            return $this->next->handlerRequest($type, $message);
        }
    }
}

header('Content-Type: text/plain');

$a=new HandlerA;
$b=new HandlerB;
$a->setNextHandler($b);

$a->handlerRequest("a","frefre");
$a->handlerRequest("b","thrt");
$a->handlerRequest("c","rjytjy");
